﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using Traceability_Management.Domain;
using Traceability_Management.ErrorCode;
using Traceability_Management.Infrastructure.Interfaces;
using Traceability_Management.Read.Api.Application.Command.RabitsWay;

namespace Traceability_Management.Read.Api.Application.CommandHandler.RabitsWay
{
    public class BatchShowCommandHandler : IRequestHandler<BatchShowCommand, ApiResult<string>>
    {
        IBaseReponsitory<Rabits> Rabits;

        public BatchShowCommandHandler(IBaseReponsitory<Rabits> Rabits)
        {
            this.Rabits = Rabits;
        }

        public async Task<ApiResult<string>> Handle(BatchShowCommand request, CancellationToken cancellationToken)
        {
            var Nid = 1;
            var Batch = await Rabits.GetQuery().OrderByDescending(c => c.Rabit_Batch).FirstOrDefaultAsync();
            if (Batch != null)
            {
                Nid = Convert.ToInt32(Batch.Rabit_Batch.Replace("Bc", ""));
                Nid++;
            }
            var Rabit_Batch = "Bc" + Nid.ToString().PadLeft(3, '0');
            return new ApiResult<string>
            {
                Code=ApiEnum.Success,
                Message="获取成功",
                Data=Rabit_Batch
            };
        }
    }
}
